#include<iostream>
#include<mutex>
#include<thread>

void Print_One(int n, int& x, std::mutex& mtx)
{
	mtx.lock();
	for (int i = 0; i < n; i++)
	{
		x++;
	}
	mtx.unlock();
}
void Print_Two(int n, int* x, std::mutex* mtx)
{
	mtx->lock();
	for (int i = 0; i < n; i++)
	{

		(*x)++;
	}
	mtx->unlock();
}
int main()
{
	int x = 0;
	std::mutex mtx;
	std::thread t1(Print_One, 10000000, std::ref(x), std::ref(mtx));
	std::thread t2(Print_Two, 20000000, &x, &mtx);
	std::thread t3(
		[&](int n) {mtx.lock();
	for (int i = 0; i < n; i++)
	{

		x++;
	}
	mtx.unlock(); }, 10000000);

	std::cout << "t1 tid-> " << t1.get_id() << std::endl;
	std::cout << "t2 tid-> " << t2.get_id() << std::endl;
	std::cout << "main tid-> " << std::this_thread::get_id() << std::endl;

	t1.join();
	t2.join();
	t3.join();
	std::cout << "x val is " << x << std::endl;
	return 0;
}